package com.xoms.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xoms.admin.controller.res.bean.RProDepositBean;
import com.xoms.admin.controller.res.bean.RProDepositDetailBean;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.model.ProDepositBean;
import com.xoms.admin.model.ProDepositDetailBean;
import com.xoms.admin.persist.dao.ProDepositDao;
import com.xoms.admin.service.ProDepositService;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.DateTimeUtil;

@Service
public class ProDepositServiceImpl implements  ProDepositService {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	ProDepositDao proDepositImpl;

	@Override
	public PageResult<RProDepositBean> getDeposits(AccountBean account, int page, int pageSize, String startTime,
			String endTime, String storeId) {
		PageResult<RProDepositBean> result = new PageResult<RProDepositBean>();
		List<RProDepositBean> resList = new ArrayList<RProDepositBean>();
		// 获取寄存数量
		int count = proDepositImpl.selectCount(startTime, endTime, storeId);
		if(count > 0) {
			//查询寄存列表
			List<ProDepositBean> lists = proDepositImpl.selectList(page, pageSize, startTime, endTime, storeId);

			if (lists != null) {
				RProDepositBean rb = null;
				for (ProDepositBean pb : lists) {
					rb=parse(pb);
					resList.add(rb);
				}
			}
		}
		
		result.setData(resList);
		result.setCount(count);
		return result;
	}

	/**
	 * 格式化数据
	 * @param pd
	 * @return
	 */
	private RProDepositBean parse(ProDepositBean pd) {
		RProDepositBean res = new RProDepositBean();
		res.setAccount(pd.getAccount());
		res.setAccountId(pd.getAccountId());
		res.setDepositCode(pd.getDepositCode());
		res.setDid(pd.getDid());
		res.setIsMember(pd.getIsMember());
		res.setMemberNo(pd.getMemberNo());
		res.setName(pd.getName());
		res.setPhoneNum(pd.getPhoneNum());
		res.setRemark(pd.getRemark());
		res.setStatus(pd.getStatus());
		res.setStoreHouse(pd.getStoreHouse());
		res.setStoreHouseId(pd.getStoreHouseId());
		res.setOverTime(DateTimeUtil.formatTimestamp(pd.getOverTime()));
		res.setTakeOutTime(DateTimeUtil.formatTimestamp(pd.getTakeOutTime()));
		res.setCreateTime(DateTimeUtil.formatTimestamp(pd.getCreateTime()));
		return res;
	}

	@Override
	public PageResult<RProDepositDetailBean> getDepositDetails(AccountBean account, String key, int page, int pageSize,
			String did, int paging) {
		PageResult<RProDepositDetailBean> result = new PageResult<RProDepositDetailBean>();
		List<RProDepositDetailBean> resList = new ArrayList<RProDepositDetailBean>();
		int count = proDepositImpl.selectDetailCount(key, did);
		if(count > 0) {
			//查询寄存单列表
			List<ProDepositDetailBean> lists = proDepositImpl.selectDetailList(key, did, page, pageSize, paging);
			if (lists != null) {
				RProDepositDetailBean rb = null;
				for (ProDepositDetailBean br : lists) {
					rb = new RProDepositDetailBean();
					rb = parseDetail(br);
					resList.add(rb);
				}
			}
		}
		result.setData(resList);
		result.setCount(count);
		return result;
	}

	private RProDepositDetailBean parseDetail(ProDepositDetailBean pd) {
		RProDepositDetailBean res = new RProDepositDetailBean();
		res.setDepositId(pd.getDepositId());
		res.setOverTime(DateTimeUtil.formatTimestamp(pd.getOverTime()));
		res.setCategory(pd.getCategory());
		res.setNumber(pd.getNumber());
		res.setProduct(pd.getProduct());
		res.setProductBar(pd.getProductBar());
		res.setProductId(pd.getProductId());
		res.setUnit(pd.getUnit());
		return res;
	}

	
}
